import {BaseCrudApi} from "~/api/util/BaseService";
import {Account} from "~/api/system/account/types";
import {reactive} from "vue";
import {JsonResult} from "~/api/types";
import {ElMessage} from "element-plus";

export class AccountApi extends BaseCrudApi<Account> {

    baseUrl = "/system/account";

    userAccountState = reactive({
        visible: false,
        loading: false,
        data: [],
    });

    queryByUserId(userId: string) {
        this.userAccountState.loading = true;
        this.get<any>("/queryByUserId", {params: {userId: userId}}).then(res => {
            if (res.data) {
                this.userAccountState.data = res.data;
                this.userAccountState.visible = true;
            }
        }).finally(() => {
            this.userAccountState.loading = false;
        });
    }

    closeAccount() {
        this.userAccountState.visible = false;
    }

    updateEnabled(id: string, entity: any): Promise<JsonResult<Account>> {
        return this.post(`/updateEnabled/${id}`, entity);
    }

    updateAccountNonExpired(id: string, entity: any): Promise<JsonResult<Account>> {
        return this.post(`/updateAccountNonExpired/${id}`, entity);
    }

    resetPassword(id: string) {
        this.post(`/resetPassword/${id}`).then(res => {
            if (res.ok) {
                ElMessage.success(res.data);
            }
        });
    }


}
